//
//  CYProfitsCalculatorView.m
//  cyjf
//
//  Created by tauCross on 2017/5/14.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYProfitsCalculatorView.h"

#import <TCNoActionControl.h>
#import <IQKeyboardManager.h>



@interface CYProfitsCalculatorView ()
{
    BOOL IQKeyboardEnable;
    BOOL IQKeyboardEnableAutoToolbar;
}

@property(nonatomic, strong)UIButton *maskButton;
@property(nonatomic, strong)UIView *contentView;
@property(nonatomic, strong)UIImageView *rateImageView;
@property(nonatomic, strong)UILabel *rateLabel;
@property(nonatomic, strong)UIImageView *dueImageView;
@property(nonatomic, strong)UILabel *dueLabel;
@property(nonatomic, strong)UILabel *profitsLabel;
@property(nonatomic, strong)UITextField *textField;

@end


@implementation CYProfitsCalculatorView

- (void)dealloc
{
    
}

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if(self)
    {
        [self loadUI];
        [self loadRAC];
    }
    return self;
}

- (void)loadUI
{
    self.maskButton = [[UIButton alloc] initWithFrame:self.bounds];
    self.maskButton.backgroundColor = HEXCOLORA(@"000000", 0.5);
    self.maskButton.alpha = 0;
    [self.maskButton addTarget:self action:@selector(onMaskButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.maskButton];
    
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height, self.width, 230)];
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.contentView];
    {
        UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 55, 50)];
        [closeButton addTarget:self action:@selector(onCloseButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        [closeButton setImage:CYImage(@"public_nav_close") forState:UIControlStateNormal];
        [self.contentView addSubview:closeButton];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"收益计算器";
        titleLabel.font = [UIFont systemFontOfSize:16];
        titleLabel.textColor = HEXCOLOR(@"444444");
        [titleLabel sizeToFit];
        titleLabel.centerX = self.contentView.width / 2;
        titleLabel.centerY = 25;
        [self.contentView addSubview:titleLabel];
        
        UIView *line = [UIView cy_lineWithColor:HEXCOLOR(@"ECECEC")];
        line.width = self.contentView.width;
        line.height = 1 / SCREEN_SCALE;
        line.bottom = 50;
        line.left = 0;
        [self.contentView addSubview:line];
        
        self.rateImageView = [[UIImageView alloc] initWithImage:CYImage(@"profits_calculator_rate")];
        [self.contentView addSubview:self.rateImageView];
        
        self.dueImageView = [[UIImageView alloc] initWithImage:CYImage(@"profits_calculator_due")];
        [self.contentView addSubview:self.dueImageView];
    
        self.rateLabel = [[UILabel alloc] init];
        self.rateLabel.font = [UIFont systemFontOfSize:14];
        self.rateLabel.textColor = HEXCOLOR(@"999999");
        [self.contentView addSubview:self.rateLabel];
        
        self.dueLabel = [[UILabel alloc] init];
        self.dueLabel.font = [UIFont systemFontOfSize:14];
        self.dueLabel.textColor = HEXCOLOR(@"999999");
        [self.contentView addSubview:self.dueLabel];
        
        line = [UIView cy_lineWithColor:HEXCOLOR(@"CCCCCC")];
        line.width = self.contentView.width - 40;
        line.height = 1 / SCREEN_SCALE;
        line.bottom = 143;
        line.centerX = self.contentView.width / 2;
        [self.contentView addSubview:line];
        
        UILabel *amountLabel = [[UILabel alloc] init];
        amountLabel.text = @"投资金额";
        amountLabel.font = [UIFont systemFontOfSize:16];
        amountLabel.textColor = HEXCOLOR(@"444444");
        [amountLabel sizeToFit];
        amountLabel.left = 20;
        amountLabel.centerY = 124;
        [self.contentView addSubview:amountLabel];
        
        UILabel *unit = [[UILabel alloc] init];
        unit.text = @"元";
        unit.font = [UIFont systemFontOfSize:16];
        unit.textColor = HEXCOLOR(@"444444");
        [unit sizeToFit];
        unit.centerY = amountLabel.centerY;
        unit.right = self.contentView.width - 20;
        [self.contentView addSubview:unit];
        
        UITextField *textFiled = [[TCNoActionTextField alloc] init];
        textFiled.textColor = HEXCOLOR(@"444444");
        textFiled.keyboardType = UIKeyboardTypeNumberPad;
        textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入投资金额" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName : HEXCOLOR(@"999999")}];
        textFiled.font = [UIFont systemFontOfSize:16];
        textFiled.width = self.width - 145;
        textFiled.height = 38;
        textFiled.centerY = amountLabel.centerY;
        textFiled.left = 104;
        [textFiled addTarget:self action:@selector(onTextFieldEditingChanged) forControlEvents:UIControlEventEditingChanged];
        [self.contentView addSubview:textFiled];
        self.textField = textFiled;
        
        UILabel *profitsLabel = [[UILabel alloc] init];
        profitsLabel.text = @"到期收益";
        profitsLabel.font = [UIFont systemFontOfSize:16];
        profitsLabel.textColor = HEXCOLOR(@"444444");
        [profitsLabel sizeToFit];
        profitsLabel.left = 20;
        profitsLabel.centerY = 176;
        [self.contentView addSubview:profitsLabel];
        
        self.profitsLabel = [[UILabel alloc] init];
        self.profitsLabel.text = @"0.00";
        self.profitsLabel.font = [UIFont systemFontOfSize:16];
        self.profitsLabel.textColor = APP_DEFAULT_COLOR_RED;
        self.profitsLabel.frame = textFiled.frame;
        self.profitsLabel.centerY = profitsLabel.centerY;
        [self.contentView addSubview:self.profitsLabel];
        
        unit = [[UILabel alloc] init];
        unit.text = @"元";
        unit.font = [UIFont systemFontOfSize:16];
        unit.textColor = HEXCOLOR(@"444444");
        [unit sizeToFit];
        unit.centerY = profitsLabel.centerY;
        unit.right = self.contentView.width - 20;
        [self.contentView addSubview:unit];
        
        line = [UIView cy_lineWithColor:HEXCOLOR(@"CCCCCC")];
        line.width = self.contentView.width;
        line.height = 1 / SCREEN_SCALE;
        line.bottom = 195;
        [self.contentView addSubview:line];
        
        UILabel *tip = [[UILabel alloc] init];
        tip.textColor = HEXCOLOR(@"999999");
        tip.text = @"*计算结果仅供参考";
        tip.font = [UIFont systemFontOfSize:14];
        [tip sizeToFit];
        tip.right = self.contentView.width - 20;
        tip.centerY = 213;
        [self.contentView addSubview:tip];
        
        line = [UIView cy_lineWithColor:HEXCOLOR(@"8C8C8C")];
        line.width = self.contentView.width;
        line.height = 1 / SCREEN_SCALE;
        line.left = 0;
        line.bottom = self.contentView.height;
        [self.contentView addSubview:line];
    }
}

- (void)loadRAC
{
    @weakify(self);
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillChangeFrameNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *notification) {
        __block BOOL isShow = NO;
        if([notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y < SCREEN_HEIGHT)
        {
            isShow = YES;
        }
        [UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]
                              delay:0
                            options:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]
                         animations:^{
                             @strongify(self);
                             if(isShow)
                             {
                                 self.maskButton.alpha = 1;
                                 self.contentView.bottom = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
                             }
                             else
                             {
                                 self.maskButton.alpha = 0;
                                 self.contentView.top = self.height;
                             }
                         }
                         completion:^(BOOL finished) {
                             @strongify(self);
                             if(isShow == NO)
                             {
                                 [self removeFromSuperview];
                             }
                         }];
    }];
    
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:CYHideAllCustomViewOnWindowNotification object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
        @strongify(self);
        [self closeAction];
    }];
}

- (void)reloadInfos
{
    self.rateLabel.text = [NSString stringWithFormat:@"年化利率:%.2f%%", self.rate.doubleValue * 100];
    self.dueLabel.text = [NSString stringWithFormat:@"收益期限:%@天", self.due];
    
    [self.rateLabel sizeToFit];
    [self.dueLabel sizeToFit];
    
    CGFloat width =  - 4;
    width += self.rateImageView.width;
    width += 1;
    width += self.rateLabel.width;
    width += 16;
    width += self.dueImageView.width;
    width += 1;
    width += self.dueLabel.width;
    CGFloat left = (self.width - width) / 2 - 4;
    self.rateImageView.left = left;
    left = self.rateImageView.right + 1;
    self.rateLabel.left = left;
    left = self.rateLabel.right + 16;
    self.dueImageView.left = left;
    left = self.dueImageView.right + 1;
    self.dueLabel.left = left;
    
    self.rateImageView.centerY = 77;
    self.rateLabel.centerY = self.rateImageView.centerY;
    self.dueImageView.centerY = self.rateImageView.centerY;
    self.dueLabel.centerY = self.rateImageView.centerY;
}

#pragma mark - Actions
- (void)closeAction
{
    [self endEditing:YES];
    [IQKeyboardManager sharedManager].enable = IQKeyboardEnable;
    [IQKeyboardManager sharedManager].enableAutoToolbar = IQKeyboardEnableAutoToolbar;
}

- (void)show
{
    IQKeyboardEnable = [IQKeyboardManager sharedManager].enable;
    IQKeyboardEnableAutoToolbar = [IQKeyboardManager sharedManager].enableAutoToolbar;
    [IQKeyboardManager sharedManager].enable = NO;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    [self.textField becomeFirstResponder];
}

#pragma mark - Control Events
- (void)onMaskButtonTouchUpInside
{
    [self closeAction];
}

- (void)onCloseButtonTouchUpInside
{
    [self closeAction];
}

- (void)onTextFieldEditingChanged
{
    double amount = [self.textField.text doubleValue];
    double profits = amount * self.rate.doubleValue * self.due.integerValue / CY_YEAR_DAY;
    self.profitsLabel.text = [@(profits) amountString];
}

@end
